var indentPrice = new Number(0);
$(function() {
	// 手机号码验证
	jQuery.validator
			.addMethod(
					"isMobile",
					function(value, element) {
						var length = value.length;
						var mobile = /^(13[0-9]{9})|(18[0-9]{9})|(14[0-9]{9})|(17[0-9]{9})|(15[0-9]{9})$/;
						var re = /^0\d{2,3}-?\d{7,8}$/;
						return this.optional(element)
								|| (length == 11 && mobile.test(value))
								|| re.test(value);
					}, "请正确填写您的电话号码");
	validateAddMeasureForm();
	$("input[name='addPrice']").TouchSpin({
		min : 0,
		max : 1000,
		//step :5,
		boostat : 5,
		maxboostedstep : 10,
		postfix : '元'
	});
	$("#Province>option").first().nextAll().remove();
	$.post("../territory/selectAvailableProvince", function(data) {
		// 这是所有的省份
		$.each(data, function(i, option) {
			$("#Province").append(
					'<option value="' + option.code + '">' + option.province
							+ '</option>');
		});
	});

});
// 查看测量订单的预览
function viewIndentPriceDetailModalOpen() {
	if ($("#addIndentForm").valid()) {
		var customerRoomNumber = $("input[name='roomNumber']").val();
		var provinceCode = $("#Province").val();
		var cityCode = $("#City").val();
		$.post("../cityCostRule/queryCostRuleByCityCode", {
			cityCode : provinceCode + "/" + cityCode
		}, function(data) {
			var celiangMoney = 0;
			if (customerRoomNumber <= data.meaBaseNum) {
				celiangMoney = data.meaStartingFare;
			} else if (customerRoomNumber > data.meaBaseNum
					&& customerRoomNumber <= data.meaBeyondNumTwo) {
				celiangMoney = data.meaStartingFare + data.meaBeyUnitPriceOne
						* (customerRoomNumber - data.meaBaseNum);
			} else {
				celiangMoney = data.meaStartingFare + data.meaBeyUnitPriceOne
						* (data.meaBeyondNumTwo - data.meaBaseNum)
						+ data.meaBeyUnitPriceTwo
						* (customerRoomNumber - data.meaBeyondNumTwo);
			}
			$('#txt_view_roomNumber').val(customerRoomNumber);
			$('#txt_view_basicRoomNumber').val(data.meaBaseNum);
			$('#txt_view_basicMeasureMoney').val(data.meaStartingFare);
			$('#txt_view_oneSection').val(
					data.meaBeyondNumOne + 1 + "~" + data.meaBeyondNumTwo
							+ "（间）为 " + data.meaBeyUnitPriceOne + " 元/单");
			$('#txt_view_twoSection').val(
					data.meaBeyondNumTwo + 1 + "（间）已以上为 "
							+ data.meaBeyUnitPriceTwo + " 元/单");
			$('#txt_view_customerMeasureMoney').val(celiangMoney);
			$("#viewIndentPriceDetailModal").modal('show');
		});
	}
}

function validateAddMeasureForm() {
	$("#addIndentForm").validate({
		rules : {
			name : "required",
			phone : {
				required : true,
				isMobile : true
			},
			subscribeTime : {
				required : true,
			},
			roomNumB : {
				required : true,
				minlength : 1
			},
			addPrice : {
				number : true,
				min : 0
			},
			province : {
				required : true,
				max : 999998
			},
			city : {
				required : true,
				max : 999998
			},
			country : {
				required : true,
				max : 999998
			},
			measureAddress : {
				required : true,
				minlength : 5
			}
		},
		errorClass : "validateFontSize error",
		messages : {
			name : {
				required : "请输入顾客姓名"
			},
			phone : {
				required : "请输入电话",
				isMobile : "请正确填写您的电话号码"
			},
			subscribeTime : {
				required : "请输入测量时间",
			},
			roomNumB : {
				required : "请输入测量房间数",
				minlength : "房间数长度至少为1"
			},
			addPrice : {
				number : "请输入数字",
				min : "请输入大于等于0的数"
			},
			province : {
				max : "省未填"
			},
			city : {
				max : "市未填"
			},
			country : {
				max : "区未填"
			},
			measureAddress : {
				required : "请输入详细地址",
				minlength : "详细地址长度至少为5"
			}
		}
	});
}

// 当省改变的时候 改变这个省的开通的城市
$("#Province").change(
		function() {
			// 清空市县
			$("#City>option").first().nextAll().remove();
			$("#countrySelect>option").first().nextAll().remove();
			$.post("../territory/selectAvailableCity", {
				provinceCode : $("#Province").val()
			},
					function(data) {
						if (data && data.length > 0) {
							$.each(data, function(i, option) {
								$("#City").append(
										'<option value="' + option.code + '">'
												+ option.city + '</option>');
							});
							$("#City>option:nth-child(2)").attr("selected",
									"selected"); // 给市默认赋值为第一个；
							var city = $("#City :selected").val();
							$.post("../territory/selectAvailableCountry", {
								cityCode : city
							}, function(data) {
								if (data && data.length > 0) {
									$.each(data, function(i, option) {
										$("#countrySelect").append(
												'<option value="' + option.code
														+ '">' + option.country
														+ '</option>');
									});
									$("#countrySelect>option:nth-child(2)")
											.attr("selected", "selected"); // 给县默认赋值为第一个；
								}
							});
						}
					});
		});
$("#City").change(
		function() {
			$("#countrySelect>option").first().nextAll().remove();
			$.post("../territory/selectAvailableCountry", {
				cityCode : $("#City").val()
			}, function(data) {
				$.each(data, function(i, option) {
					$("#countrySelect").append(
							'<option value="' + option.code + '">'
									+ option.country + '</option>');
				});
				$("#countrySelect>option:nth-child(2)").attr("selected",
						"selected"); // 给县默认赋值为第一个；
			});
		});

function makeMeasureIndent(object) {
	if ($("#addIndentForm").valid()) {
		$(object).removeClass('btn-info');
		$(object).addClass('btn-warning');
		$(object).attr('disabled', 'disabled');
		$(object).html("生成中...");
		$.post("../indent/makeMersureIndent", $("#addIndentForm").serialize(),
				function(data) {
					if (data.status == 0) {
						$("input[name='indentID']").val(data.data);
						$(object).removeClass('btn-warning');
						$(object).addClass('btn-info');
						$(object).removeAttr('disabled');
						$(object).html("生成订单");
						countIndentPrice();
						$("#addIndentForm")[0].reset();
						$("#City>option:nth-child(1)").attr("selected",
								"selected"); // 给县默认赋值为第一个；
						$("#countrySelect>option:nth-child(1)").attr(
								"selected", "selected"); // 给县默认赋值为第一个；
						// 计算原本的订单价格
						$("#payIndentModal").modal('show');
					}
				});
	}
}
// 支付订单信息
function payMeasureIndent(url) {
	$("#payIndentForm").attr("action",url);
	$("#payIndentForm").submit();
	$("#payIndentForm")[0].reset();
	$("#payIndentModal").modal('hide');
}
// 关闭付款窗口
function closePayIndentModal() {
	
	Ewin.confirm({
		message : "生成的订单可以在我的未付款订单中查询和付款"
	}).on(function(e) {
		if (!e) {
			window.location.reload();
			return;
		} else {
			window.location.href = '../indent/measureList/pay.htm'
		}
	})
}

// 预览订单的价格
$("#payIndentForm input").change(function() {
	countTotalIndentPrice();
});
// 计算订单的总价格
function countTotalIndentPrice() {
	var totalIndentPrice = new Number(indentPrice);
	totalIndentPrice = parseInt(totalIndentPrice)
			+ parseInt($("#payIndentForm input[name='addPrice']").val());
	if ($("#payIndentForm input[name='relievedBao']:checked").val() == 1) {
		totalIndentPrice = totalIndentPrice + 5;
	}
	$("#payIndentForm #totalPrice").val(totalIndentPrice);
}

// 计算订单的价格
function countIndentPrice() {
	var customerRoomNumber = $("input[name='roomNumber']").val();
	var provinceCode = $("#Province").val();
	var cityCode = $("#City").val();
	$.post("../cityCostRule/queryCostRuleByCityCode", {
		cityCode : provinceCode + "/" + cityCode
	}, function(data) {
		var celiangMoney = 0;
		if (customerRoomNumber <= data.meaBaseNum) {
			celiangMoney = data.meaStartingFare;
		} else if (customerRoomNumber > data.meaBaseNum
				&& customerRoomNumber <= data.meaBeyondNumTwo) {
			celiangMoney = data.meaStartingFare + data.meaBeyUnitPriceOne
					* (customerRoomNumber - data.meaBaseNum);
		} else {
			celiangMoney = data.meaStartingFare + data.meaBeyUnitPriceOne
					* (data.meaBeyondNumTwo - data.meaBaseNum)
					+ data.meaBeyUnitPriceTwo
					* (customerRoomNumber - data.meaBeyondNumTwo);
		}
		indentPrice = celiangMoney;
		$("#payIndentForm #totalPrice").val(indentPrice + 5);
	});
}